package com.yxhl.zoume.core.tripsmgmt.ui.fragment;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import com.google.android.flexbox.FlexboxLayout;
import com.yxhl.protobuf.BizOrder;
import com.yxhl.zoume.R;
import com.yxhl.zoume.UIConstants;
import com.yxhl.zoume.common.ui.custom.CheckBoxLayout;
import com.yxhl.zoume.common.ui.fragment.BaseFragment;
import com.yxhl.zoume.core.officialcar.activity.OfficialCarScheduleActivity;
import com.yxhl.zoume.core.func.appraisal.model.CommentOnDriverEvent;
import com.yxhl.zoume.core.tripsmgmt.info.CommentEntrance;
import com.yxhl.zoume.core.tripsmgmt.presenter.unit.CommentOnDriverPresenter;
import com.yxhl.zoume.core.tripsmgmt.ui.activity.CommentOnDriverActivity;
import com.yxhl.zoume.core.tripsmgmt.view.unit.CommentOnDriverView;
import com.yxhl.zoume.data.rxbus.RxBus;
import com.yxhl.zoume.di.component.trip.DaggerTripComponent;
import com.yxhl.zoume.di.module.ActivityModule;
import com.yxhl.zoume.di.module.trip.TripModule;
import com.yxhl.zoume.utils.PromptUtils;
import com.yxhl.zoume.utils.StringUtils;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * created by sugarya
 */
public class CommentOnDriverFragment extends BaseFragment implements CommentOnDriverView {

    private static final String TAG = "CommentOnDriverFrag";

    @BindView(R.id.tv_trip_comment_driver_info)
    TextView commentDriverInfoTv;
    @BindView(R.id.tv_trip_comment_driver_point)
    TextView commentDriverPoint;

    @BindView(R.id.cbl_trip_comment_judge)
    CheckBoxLayout judgeCbl;

    @BindView(R.id.flex_trip_comment_tag_container)
    FlexboxLayout tagContainerFlex;

    @BindView(R.id.ckb_trip_comment_tag_1)
    CheckBox tagFirstCkb;
    @BindView(R.id.ckb_trip_comment_tag_2)
    CheckBox tagSecondCkb;
    @BindView(R.id.ckb_trip_comment_tag_3)
    CheckBox tagThirdCkb;
    @BindView(R.id.ckb_trip_comment_tag_4)
    CheckBox tagFourthCkb;
    @BindView(R.id.ckb_trip_comment_tag_5)
    CheckBox tagFifthCkb;

    @BindView(R.id.btn_trip_comment_judge)
    Button judgeBtn;

    //arguments
    @Inject
    CommentOnDriverPresenter mCommentOnDriverPresenter;

    private BizOrder mBizOrder;

    private CommentEntrance mCommentEntrance;
    private int mJudgePoint;

    public CommentOnDriverFragment() {
        // Required empty public constructor
    }

    public static CommentOnDriverFragment newInstance(BizOrder bizOrder, CommentEntrance entrance) {

        Bundle args = new Bundle();
        args.putSerializable(UIConstants.TripContainer.ARGUMENT_KEY_COMMENT_ON_DRIVER, bizOrder);
        args.putSerializable(UIConstants.TripContainer.ARGUMENT_KEY_TRIP_COMMENT_ENTRANCE, entrance);
        CommentOnDriverFragment fragment = new CommentOnDriverFragment();
        fragment.setArguments(args);
        return fragment;
    }

    private void handleArguments() {
        Bundle arguments = getArguments();
        if (arguments != null) {
            mBizOrder = (BizOrder) arguments.getSerializable(UIConstants.TripContainer.ARGUMENT_KEY_COMMENT_ON_DRIVER);
            mCommentEntrance = (CommentEntrance) arguments.getSerializable(UIConstants.TripContainer.ARGUMENT_KEY_TRIP_COMMENT_ENTRANCE);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        handleArguments();
        mCommentOnDriverPresenter.attachView(this);
        bindData(mBizOrder);
        setListener();
        return view;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            setupToolBar();
        }
    }

    private void bindData(BizOrder bizOrder) {
        String calledName = StringUtils.getCalledName(bizOrder.getDriverName());
        String vehicleNo = bizOrder.getVehicleNo();
        commentDriverInfoTv.setText(calledName + "  " + vehicleNo);

        String avgDriverPoint = bizOrder.getAvgDriverPoint();
        if (!TextUtils.isEmpty(avgDriverPoint)) {
            commentDriverPoint.setText("综合评分:  " + avgDriverPoint);
        } else {
            commentDriverPoint.setText("综合评分:  " + 0);
        }
    }

    private void setListener() {
        judgeCbl.setOnSelectedListener(new CheckBoxLayout.onSelectedListener() {
            @Override
            public void onSelected(View view, int position) {
                judgeBtn.setBackgroundResource(R.drawable.bg_trip_comment_submit);
                judgeBtn.setClickable(true);
            }
        });
    }

    @OnClick(R.id.btn_trip_comment_judge)
    public void onJudgeSubmitClick(View view) {
        String judgeContent = getCheckedTagContent();
        mJudgePoint = (int) judgeCbl.getCheckedCount();

        mCommentOnDriverPresenter.commentOnDriver(mBizOrder.getOrderSerial(), mJudgePoint, judgeContent);
    }

    /**
     * 获取选中的标签内容
     *
     * @return
     */
    private String getCheckedTagContent() {
        StringBuilder sb = new StringBuilder();
        int childCount = tagContainerFlex.getChildCount();
        for (int i = 0; i < childCount; i++) {
            CheckBox checkBox = (CheckBox) tagContainerFlex.getChildAt(i);
            if (checkBox.isChecked()) {
                String s = checkBox.getText().toString().trim();
                sb.append(s);
            }
        }
        return sb.toString();
    }


    @Override
    public void closeCommentOnCompleted() {
    }

    @Override
    public void renderCommentOnError(String msg) {
        if (!TextUtils.isEmpty(msg)) {
            PromptUtils.showShort(mActivity, msg);
        }
    }

    @Override
    public void renderCommentOnSuccess() {
        RxBus.getInstance().send(new CommentOnDriverEvent(mCommentEntrance, mJudgePoint));
        PromptUtils.showWhenCommentOnSuccess(mActivity, "感谢您的评价!");
        mActivity.finish();
        mActivity.overridePendingTransition(R.anim.noanimation, R.anim.right_out);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        mCommentOnDriverPresenter.onDestroy();
    }

    @Override
    protected void initializeInjector() {
        DaggerTripComponent
                .builder()
                .tripModule(new TripModule())
                .activityModule(new ActivityModule(mActivity))
                .appComponent(getAppComponent())
                .build()
                .inject(this);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_comment_on_driver;
    }

    @Override
    protected void setupToolBar() {
        if (mActivity instanceof CommentOnDriverActivity) {
            ((CommentOnDriverActivity) mActivity).setToolbarTitle("评价");
        }

        if (mActivity instanceof OfficialCarScheduleActivity) {
            ((OfficialCarScheduleActivity) mActivity).setToolbarTitle("评价");
        }
    }

}
